package com.wl.java_spring_boot01.modules.test.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wl.java_spring_boot01.aspect.ServiceAnnotation;
import com.wl.java_spring_boot01.modules.common.vo.Result;
import com.wl.java_spring_boot01.modules.common.vo.SearchVo;
import com.wl.java_spring_boot01.modules.test.dao.CityDao;
import com.wl.java_spring_boot01.modules.test.entity.City;
import com.wl.java_spring_boot01.modules.test.service.CityService;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class CityServiceImpl implements CityService {

    @Autowired
    private CityDao cityDao;

    //通过id查城市
    @ServiceAnnotation(value = "bbb")
    @Override
    public List<City> getCitiesByCountryId(int countryId) {
//        return cityDao.getCitiesByCountryId(countryId);
        return Optional
                .ofNullable(cityDao.getCitiesByCountryId(countryId))
                .orElse(Collections.emptyList());
    }

    //分页查询--显示城市
    @Override
    public PageInfo<City> getCitiesBySearchVo(int countryId, SearchVo searchVo) {
        PageHelper.startPage(searchVo.getCurrentPage(),searchVo.getPageSize());
        return new PageInfo<City>(
                Optional.ofNullable(cityDao.getCitiesByCountryId(countryId))
                        .orElse(Collections.emptyList())
        );
    }

    //通过searchVo查城市--脚本的多条件查询
    @Override
    public PageInfo<City> getCitiesBySearchVo(SearchVo searchVo) {
        //SearchVo对象初始化
        searchVo.initSearchVo();
        //开启分页
        PageHelper.startPage(searchVo.getCurrentPage(),searchVo.getPageSize());
        //返回一个分页对象
        return new PageInfo<>(
                Optional.ofNullable(cityDao.getCitiesBySearchVo(searchVo))
                        .orElse(Collections.emptyList())
        );
    }

    //增加
    @Override
    public Result<City> insertCity(City city) {
        cityDao.insertCity(city);
        return new Result<>(Result.ResultStatus.SUCCESS.status,"insert success",city);
    }

    //修改
    @Override
    public Result<City> updateCity(City city) {
        cityDao.updateCity(city);
        return new Result<>(Result.ResultStatus.SUCCESS.status,"update success",city);
    }

    //删除
    @Override
    public Result<City> deleteCity(int cityId) {
        cityDao.deleteCity(cityId);
        return new Result<>(Result.ResultStatus.SUCCESS.status,"delete success");
    }
}
